package com.flyco.modules.sms.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flyco.common.util.DateUtils;
import com.flyco.common.util.oConvertUtils;
import com.flyco.modules.pms.entity.PmsProduct;
import com.flyco.modules.pms.entity.PmsProductCategory;
import com.flyco.modules.pms.service.IPmsProductCategoryService;
import com.flyco.modules.pms.service.IPmsProductService;
import com.flyco.modules.sms.entity.SmsCoupon;
import com.flyco.modules.sms.entity.SmsCouponHistory;
import com.flyco.modules.sms.entity.SmsCouponProductCategoryRelation;
import com.flyco.modules.sms.entity.SmsCouponProductRelation;
import com.flyco.modules.sms.mapper.SmsCouponHistoryMapper;
import com.flyco.modules.sms.mapper.SmsCouponMapper;
import com.flyco.modules.sms.mapper.SmsCouponStoreMapper;
import com.flyco.modules.sms.model.SmsCouponModel;
import com.flyco.modules.sms.service.ISmsCouponProductCategoryRelationService;
import com.flyco.modules.sms.service.ISmsCouponProductRelationService;
import com.flyco.modules.sms.service.ISmsCouponService;
import com.flyco.modules.sms.vo.SmsCouponVO;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 优惠券管理
 * @Author: flyco
 * @Date:   2020-01-07
 * @Version: V1.0
 */
@Service
public class SmsCouponServiceImpl extends ServiceImpl<SmsCouponMapper, SmsCoupon> implements ISmsCouponService {

	@Autowired
	private SmsCouponMapper smsCouponMapper;
	@Autowired
	private SmsCouponHistoryMapper smsCouponHistoryMapper;
	@Autowired
	private SmsCouponStoreMapper smsCouponStoreMapper;
	@Autowired
	private ISmsCouponProductCategoryRelationService smsCouponProductCategoryRelationService;
	@Autowired
	private ISmsCouponProductRelationService smsCouponProductRelationService;
	@Autowired
	private IPmsProductCategoryService pmsProductCategoryService;
	@Autowired
	private IPmsProductService pmsProductService;

	@Override
	public void addRelation(SmsCouponModel smsCouponModel) {
		if ( smsCouponModel.getUseType() == 1) {
			//分类
			List<SmsCouponProductCategoryRelation> categoryRelations = new ArrayList<>();
			Collection<PmsProductCategory> productCategories = pmsProductCategoryService.listByIds(smsCouponModel.getCategoryList());
			for ( PmsProductCategory category : productCategories) {
				SmsCouponProductCategoryRelation categoryRelation = new SmsCouponProductCategoryRelation();
				categoryRelation.setCouponId(smsCouponModel.getId());
				categoryRelation.setProductCategoryId(category.getId());
				categoryRelation.setProductCategoryName(category.getName());
				categoryRelations.add(categoryRelation);
			}
			smsCouponProductCategoryRelationService.saveBatch(categoryRelations);

		} else if ( smsCouponModel.getUseType() == 2) {
			//商品
			List<SmsCouponProductRelation> productRelations = new ArrayList<>();
			Collection<PmsProduct> products = pmsProductService.listByIds(smsCouponModel.getProductList());
			for (PmsProduct product : products ) {
				SmsCouponProductRelation productRelation = new SmsCouponProductRelation();
				productRelation.setCouponId(smsCouponModel.getId());
				productRelation.setProductId(product.getId());
				productRelation.setProductName(product.getName());
				productRelation.setProductSn(product.getProductSn());
				productRelations.add(productRelation);
			}
			smsCouponProductRelationService.saveBatch(productRelations);
		}
	}


	@Override
	public void editRelation(SmsCouponModel smsCouponModel) {
		//分类
		LambdaQueryWrapper<SmsCouponProductCategoryRelation> categoryQuery = new LambdaQueryWrapper<>();
		categoryQuery.eq(SmsCouponProductCategoryRelation::getCouponId,smsCouponModel.getId());
		smsCouponProductCategoryRelationService.remove(categoryQuery);
		//商品
		LambdaQueryWrapper<SmsCouponProductRelation> productQuery = new LambdaQueryWrapper<>();
		productQuery.eq(SmsCouponProductRelation::getCouponId,smsCouponModel.getId());
		smsCouponProductRelationService.remove(productQuery);

		addRelation(smsCouponModel);
	}

	@Override
	public void formatCouponModel(SmsCouponModel smsCouponModel) {
		//smsCouponModel.setStartTime(DateUtils.beginOfDay(smsCouponModel.getStartTime()));
		//smsCouponModel.setEndTime(DateUtils.endOfDay(smsCouponModel.getEndTime()));
		if ("discount".equals(smsCouponModel.getCouponType())) {
			smsCouponModel.setGiftData(null);
		} else if ("gift".equals(smsCouponModel.getCouponType())) {
			smsCouponModel.setAmount(BigDecimal.ZERO);
		} else if ("rate".equals(smsCouponModel.getCouponType())) {
			BigDecimal a = new BigDecimal(1);
			BigDecimal b = new BigDecimal(10);
			if ( smsCouponModel.getAmount().compareTo(a) > -1 ) {
				smsCouponModel.setAmount(smsCouponModel.getAmount().divide(b));
			}
		}
		if ( smsCouponModel.getUseType() == 0) {
			//全场通用
			smsCouponModel.getCategoryList().clear();
			smsCouponModel.getProductList().clear();
		}
		else if ( smsCouponModel.getUseType() == 1) {
			//分类
			smsCouponModel.getProductList().clear();
		} else if ( smsCouponModel.getUseType() == 2) {
			//商品
			smsCouponModel.getCategoryList().clear();
		}
	}

	@Override
	public String checkCouponModel(SmsCouponModel smsCouponModel) {
		if (oConvertUtils.isEmpty(smsCouponModel)) return  "参数不可以为空";
		if (oConvertUtils.isEmpty(smsCouponModel.getCouponType())) return  "券类型不可以为空";
		if (oConvertUtils.isEmpty(smsCouponModel.getStartTime())) return  "开始时间不可以为空";
		if (oConvertUtils.isEmpty(smsCouponModel.getEndTime())) return  "截止时间不可以为空";
		if (oConvertUtils.isEmpty(smsCouponModel.getUseType())) return  "使用类型不可以为空";
		if ("discount".equals(smsCouponModel.getCouponType())) {
			//抵扣券
			if (oConvertUtils.isEmpty(smsCouponModel.getAmount())) return  "面额不可以为空";
		} else if ("gift".equals(smsCouponModel.getCouponType())) {
			//礼品券
			if (oConvertUtils.isEmpty(smsCouponModel.getGiftData())) return  "赠品不可以为空";
		}
		if ( smsCouponModel.getUseType() == 1) {
			//分类
			if (CollectionUtils.isEmpty(smsCouponModel.getCategoryList())) return "关联分类不可以为空";
		} else if ( smsCouponModel.getUseType() == 2) {
			//商品
			if (CollectionUtils.isEmpty(smsCouponModel.getProductList())) return "关联商品不可以为空";
		}
		return "";
	}

	@Override
	public List<SmsCouponProductCategoryRelation> queryCategoryRelationById (Long id) {
		LambdaQueryWrapper<SmsCouponProductCategoryRelation> categoryQuery = new LambdaQueryWrapper<>();
		categoryQuery.eq(SmsCouponProductCategoryRelation::getCouponId,id);
		return smsCouponProductCategoryRelationService.list(categoryQuery);
	}

	@Override
	public List<SmsCouponProductRelation> queryProductyRelationById (Long id) {
		LambdaQueryWrapper<SmsCouponProductRelation> productyQuery = new LambdaQueryWrapper<>();
		productyQuery.eq(SmsCouponProductRelation::getCouponId,id);
		return smsCouponProductRelationService.list(productyQuery);
	}
	
	@Override
	@Transactional
	public void saveMain(SmsCoupon smsCoupon, List<SmsCouponHistory> smsCouponHistoryList) {
		smsCouponMapper.insert(smsCoupon);
		for(SmsCouponHistory entity:smsCouponHistoryList) {
			//外键设置
			entity.setCouponId(smsCoupon.getId());
			smsCouponHistoryMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void updateMain(SmsCoupon smsCoupon,List<SmsCouponHistory> smsCouponHistoryList) {
		smsCouponMapper.updateById(smsCoupon);
		
		//1.先删除子表数据
		smsCouponHistoryMapper.deleteByMainId(smsCoupon.getId());
		
		//2.子表数据重新插入
		for(SmsCouponHistory entity:smsCouponHistoryList) {
			//外键设置
			entity.setCouponId(smsCoupon.getId());
			smsCouponHistoryMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void delMain(Long id) {
		smsCouponHistoryMapper.deleteByMainId(id);
		smsCouponMapper.deleteById(id);
		smsCouponStoreMapper.deleteByMainId(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			smsCouponHistoryMapper.deleteByMainId((Long) id);
			smsCouponStoreMapper.deleteByMainId((Long) id);
			smsCouponMapper.deleteById(id);
		}
	}

	public Page<SmsCouponVO> getCouponGrantPage(Page<SmsCouponVO> page, Wrapper<SmsCouponVO> wrapper){
		return page.setRecords(smsCouponMapper.getCouponGrantPage(page, wrapper));
	}
	
}
